<?php
/**
 * 分页
 * @param string $url
 * @param int $TotalNumber
 * @param int $MaxPerPage
 * @param int $ShowTotal
 * @param int $CurrentPage
 */
function showPage($url, $TotalNumber, $MaxPerPage, $ShowTotal, $CurrentPage = 1){
	
	if(0==$TotalNumber || 0==$MaxPerPage || !$MaxPerPage) {
		return "";
	}
	if(0 == ($TotalNumber % $MaxPerPage)) {
		$TotalPage = intval($TotalNumber/$MaxPerPage);
	} else {
		$TotalPage = intval($TotalNumber/$MaxPerPage)+1;
	}
	
	if($CurrentPage > $TotalPage) {
		$CurrentPage = $TotalPage;
	} 
	$strTemp = '<div class="row-fluid">';
	if(true == $ShowTotal) {
		$strTemp .= '<div class="span6"><div class="dataTables_info" id="sample_2_info">共：<b>' . $TotalNumber . '</b>条记录</div></div>';
	}
	$strTemp .= '<div class="span6"><div class="dataTables_paginate paging_bootstrap pagination"><ul>';
	
	if ($TotalPage ==1) {
		$strTemp .= '<li class="prev disabled"><a href="#">← <span class="hidden-480">Prev</span></a></li><li class="active"><a href="#">1</a></li><li class="next disabled"><a href="#"><span class="hidden-480">Next</span> → </a></li></ul>';
	} else {
		if ($CurrentPage == 1) {
			$strTemp .= '<li class="prev disabled"><a href="#">← <span class="hidden-480">Prev</span></a></li>';
		} else {
			$strTemp .= '<li class="prev"><a href="' . $url . '&page=' . ($CurrentPage - 1) . '">← <span class="hidden-480">Prev</span></a></li>';
		}
		
		for ($i = 1; $i <= $TotalPage; $i++) {
			if ($i == $CurrentPage) {
				$strTemp .= '<li class="active"><a href="' . $url . '&page=' . $CurrentPage . '">' . $i . '</a></li>';
			} else {
				$strTemp .= '<li><a href="' . $url . '&page=' . $i . '">' . $i . '</a></li>';
			}
		}
		if ($CurrentPage == $TotalPage) {
			$strTemp .= '<li class="next disabled"><a href="#"><span class="hidden-480">Next</span> → </a></li>';
		} else {
			$strTemp .= '<li class="next"><a href="' . $url . '&page=' . ($CurrentPage + 1) . '"><span class="hidden-480">Next</span> → </a></li>';
		}
	}
	$strTemp .= '</ul></div></div></div>';
	return $strTemp;
}

/**
 * 获取图片路径
 * @param unknown $url
 * @param unknown $width
 * @param unknown $height
 * @return string
 */
function getImageUrlBySize($url, $width, $height) {
    $resUrl = '';
    if (!empty($url)) {
        $arrUrl = explode('.', $url);
        $intRand = '';//rand(1,3);
        $resUrl = 'http://img' . $intRand . '.matsumassage.cn'. $arrUrl[0] . '_' . $width . 'x' . $height . '.' . $arrUrl[1];
    }
    return $resUrl;
}

/**
 * 检验是否登陆
 * @param int $type 登陆类型， 1位外站登陆 2位一般登陆
 * @return array()
 */
function checkLogin($type = 1) {
    $objLoginMode = core::Singleton('library.module.LoginMode');
    $userInfo = $objLoginMode->checkLogin();
    if (!empty($userInfo)) {
        return $userInfo;
    } else {
        if ($type ==1) {
            $objWxMode = core::Singleton("library.module.WeixinMode");
            $uri = urlencode(M_DOMAIN . $_SERVER['REQUEST_URI']);
            $objWxMode->getCode(M_DOMAIN . '/login/wxRegist.html?uri=' . $uri);
        } else {
            header('location:' . M_DOMAIN . '/login/regist.html');
        }
    }
}

/**
 * 短信验证码验证
 * @param string $phone
 * @param string $verifyCode
 */
function checkCode($phone, $verifyCode) {
    $objRedis = core::cache()->factory('DEFAULT', _CACHE_REDIS);
    $redis_key = 'verify_code_' . $phone;
    $verifyCode = $objRedis->get($redis_key);
    if ($verifyCode == $verifyCode && !empty($verifyCode)) {
        $sendCountDay = $objRedis->set('verify_code_' . $phone . '_count_' . date('Ymd'), 0);
        return true;
    } else {
        return false;
    }
}

/**
 * 检测手机号
 * @param string $phone
 * @return boolean
 */
function checkPhone($phone) {
    if(preg_match("/^1[34578]\d{9}$/", $phone)){
        return true;
    } else {
        return false;
    }
}

/**
 * 获取随机号
 * @param int $length
 * @return string
 */
function getRandStr($length) {
    $str = '';
    $strPol = '0123456789';
    $max = strlen($strPol) - 1;
    for ($i = 0; $i < $length; $i++) {
       $str .= $strPol[rand(0,$max)];
    }
    return $str;
}

?>